﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;
using System.Data;
using System.Data.Objects;


namespace Sispe.Repository
{
    public class LineaTelefonicaRepository: MasterRepository ,ILineaTelefonicaRepository
    {
        public List<LINEA_TELEFONICA> GetAllFromLineaTelefonica()
        {
            var query = from p in context.LINEA_TELEFONICA
                        select p;
            //ObjectContext.Refresh(RefreshMode.StoreWins, query);
            return query.ToList();
        }

        public List<LINEA_TELEFONICA> GetAllFromLineaTelefonicaByDescripcion(string descripcion)
        {
            if (descripcion != string.Empty)
            {
                var query = from p in context.LINEA_TELEFONICA
                            where p.Descripcion.ToUpper().Contains(descripcion.ToUpper())
                            select p;
                //ObjectContext.Refresh(RefreshMode.StoreWins, query);
                return query.ToList();

            }
            else
            {
                var query = from p in context.LINEA_TELEFONICA
                            select p;
                //ObjectContext.Refresh(RefreshMode.StoreWins, query);
                return query.ToList();
            }
        }

        public LINEA_TELEFONICA GetFromLineaTelefonicaById(int id)
        {
            var query = from p in context.LINEA_TELEFONICA
                        where p.Id.Equals(id)
                        select p;
            //ObjectContext.Refresh(RefreshMode.StoreWins, query);
            return query.SingleOrDefault();
        }

        public void AddLineaTelefonica(LINEA_TELEFONICA lineaTelefonica)
        {
            context.LINEA_TELEFONICA.Add(lineaTelefonica);
            context.SaveChanges();
        }

        public void UpdateLineaTelefonica(LINEA_TELEFONICA lineaTelefonica)
        {
            context.Entry(lineaTelefonica).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void DeleteLineaTelefonica(int id)
        {
            var existe = context.LINEA_TELEFONICA.Where(p => p.Id.Equals(id)).SingleOrDefault();

            if (existe != null)
            {
                context.LINEA_TELEFONICA.Remove(existe);
                context.SaveChanges();
            }
        }

        public bool ExisteDuplicidad(string descripcion, int id)
        {
            var query = from p in context.LINEA_TELEFONICA
                        where p.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0
                        && p.Id != id
                        select p;

            return query.Any();
        }
    }
}
